package cn.edu.njuit.children.service.impl;

import cn.edu.njuit.children.config.BaiduAiProperties;
import com.baidu.aip.face.AipFace;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.HashMap;

/**
 * @Author guzy
 * @Description 百度实现接口
 * @Date 2022/5/24 8:15
 **/

@Component
public class BaiduAiService {

    @Autowired
    BaiduAiProperties baiduAiProperties;

    /**
     * 检测人脸照片
     *
     * @param imageBase64
     * @return 返回识别结果
     */
    public JSONObject checkFaces(String imageBase64) {
        // 初始化一个AipFace
        AipFace client = new AipFace(
                baiduAiProperties.getAppId(),
                baiduAiProperties.getApiKey(),
                baiduAiProperties.getSecretKey());
        // 传入可选参数调用接口
        HashMap options = new HashMap();
        options.put("face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,landmark72,landmark150,quality,eye_status,emotion,face_type");
        options.put("max_face_num", "10");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "NONE");

        String imageType = "BASE64";

        // 人脸检测
        JSONObject res = client.detect(imageBase64, imageType, options);
        return res;
    }


    /**
     * 检测人脸照片  通过url识别
     *
     * @param url 传入照片的网络地址
     * @return 返回识别结果
     */
    public JSONObject checkFacesUrl(String url) {
        // 初始化一个AipFace
        // 初始化一个AipFace
        AipFace client = new AipFace(
                baiduAiProperties.getAppId(),
                baiduAiProperties.getApiKey(),
                baiduAiProperties.getSecretKey());
        // 传入可选参数调用接口
        HashMap options = new HashMap();
        options.put("face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,landmark72,landmark150,quality,eye_status,emotion,face_type");
        options.put("max_face_num", "10");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "NONE");

        //之前参数名是  base64 现在要换成URL
        String imageType = "URL";

        // 人脸检测
        JSONObject res = client.detect(url, imageType, options);
        return res;
    }


    /**
     * 将获取的照片存放到BOS中
     *
     * @param fileKey     照片
     * @param inputStream 照片流
     * @return 返回一个照片网络地址
     */
    public String putFile(String fileKey, InputStream inputStream) {
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(
                baiduAiProperties.getAccessKeyId(),
                baiduAiProperties.getSecretAccessKey()));
        config.setEndpoint(baiduAiProperties.getEndpoint());
        BosClient client = new BosClient(config);

        // 以数据流形式上传Object
        PutObjectResponse putObjectResponseFromInputStream = client.putObject("children", fileKey, inputStream);
        String etag = putObjectResponseFromInputStream.getETag();
        //将照片网络地址 返回
        return etag;
    }

}
